package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.UserRequirePriResponseBean;
import com.homedo.odin.solution.model.request.mng.require.UserRequireReplyRequestBean;
import com.homedo.odin.solution.model.request.mng.require.UserRequireRequestBean;
import com.homedo.odin.solution.service.bizservice.mng.MngUserRequireService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "用户定制需求", tags = {"Requirement"},
        produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/reqs")
public class MngUserRequireController extends BaseController<MngUserRequireService> {

    @GetMapping
    @ApiOperation(value = "MNG_用户定制需求列表", response = UserRequirePriResponseBean.class, httpMethod = "GET")
    public ResultData getUserRequireList(@Valid @ModelAttribute UserRequireRequestBean requestBean) {
        if (requestBean.getStartCreateDate() != null && requestBean.getEndCreateDate() != null
                && requestBean.getStartCreateDate() > requestBean.getEndCreateDate()) {
            return ResultData.newInstance(new Meta(Meta.CODE_451.getCode(), "时间区间不合法"));
        }
        return ResultData.newInstance(baseService.getUserRequires(requestBean));
    }

    @GetMapping("{reqId}")
    @ApiOperation(value = "MNG_用户定制需求详情", response = UserRequirePriResponseBean.class, httpMethod = "GET")
    public ResultData getUserRequireDetail(
            @ApiParam(value = "id", required = true) @PathVariable(value = "reqId") Integer id) {
        if (!NumberHelper.validatePositive(id)) {
            throw new BizException(Meta.CODE_451.getCode(), "用户Id必须是正整数类型（大于0）");
        }
        UserRequirePriResponseBean responseBean = baseService.getUserRequireDetail(id);
        return ResultData.newInstance(responseBean);
    }

    @PostMapping("/{reqId}/reply")
    @ApiOperation(value = "MNG_回复用户需求", httpMethod = "POST")
    public ResultData replyUserRequire(@PathVariable("reqId") Integer reqId,
            @Valid @RequestBody UserRequireReplyRequestBean requestBean) {
        if (!NumberHelper.validatePositive(reqId)) {
            throw new BizException(Meta.CODE_451.getCode(), "需求标识必须是正整数类型（大于0）");
        }
        baseService.replyUserRequire(reqId, requestBean);
        return ResultData.newInstance();
    }
}
